package com.auerdi.ui.detail;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.auerdi.R;
import com.example.eggframe.GoHomeEvent;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.retrofit.beans.banner.HomeBanner;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

/**
 * Created by YoKeyword on 16/2/14.
 */
public class ADProductWebviewFragment extends SwipeBackFragment {


    private static HomeBanner mBanner;
    @BindView(R.id.title_left)
    View titleLeft;
    Unbinder unbinder;
    @BindView(R.id.wv_view)
    WebView wvView;
    @BindView(R.id.b_1)
    LinearLayout b1;

    public static ADProductWebviewFragment newInstance(HomeBanner banner) {

        Bundle args = new Bundle();

        mBanner = banner;

        ADProductWebviewFragment fragment = new ADProductWebviewFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ad_fragment_product_web, container, false);


        View tpview = attachToSwipeBack(view);
        unbinder = ButterKnife.bind(this, tpview);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                popTo(AdHomeTabFragment.class, false);
                EventBusActivityScope.getDefault(_mActivity).post(new GoHomeEvent());


            }
        });


        wvView.getSettings().setDefaultTextEncodingName("utf-8");

        wvView.getSettings().setJavaScriptEnabled(true);
        wvView.setWebChromeClient(new WebChromeClient());
//        wvView.setBackgroundColor(Color.rgb(96, 96, 96));
        wvView.setBackgroundColor(Color.rgb(96, 96, 96));
        wvView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 当打开新链接时，使用当前的 WebView，不会使用系统其他浏览器
                L.d("hcia", "url:" + url);
                view.loadUrl(url);
                return true;
            }


            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                L.d("hcia", "url:" + url);

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                /**
                 * 注册JavascriptInterface，其中"lee"的名字随便取，如果你用"lee"，那么在html中只要用  lee.方法名()
                 * 即可调用MyJavascriptInterface里的同名方法，参数也要一致
                 */

                L.d("hcia", "url:" + url);

                if (wvView != null)
                    wvView.addJavascriptInterface(new JsObject(), "lee");


            }


        });//添加一个页面相应监听类
        String url = "https://www.baidu.com";
        url = mBanner.getUrl();


        L.d("hcia", "url:" + url);


        wvView.loadUrl(url);

        titleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });


        return tpview;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }


    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        hideSoftInput();
    }

    class JsObject {

        @JavascriptInterface
        public void funAndroid(final String i) {
            Toast.makeText(getContext(), "通过JS调用本地方法funAndroid " + i, Toast.LENGTH_SHORT).show();
        }
    }
}
